<!--
 * @Author: Aaron
 * @Date: 2022-02-18 10:02:44
 * @LastEditors: YanMeng
 * @LastEditTime: 2022-11-16 06:52:34
 * @Description: file content
 * @FilePath: \ias_ui\src\views\coi\spuManagement\components\listText.vue
 * @Company: 沃尔玛
-->
<!--
 *列表组件主要是spu文描管理以及采销的维护使用
-->
<template>
  <div>
    <search-head
      :search-list="searchList"
      :search-data="searchData"
      :options="options"
      :btn-list="btnList"
      @handleSearch="handleSearch"
      @handleReset="handleReset"
    />
    <ess-table
      ref="table"
      :thead-list="spuListHeader"
      :other-container-box="otherContainerBox"
      :total="total"
      :data-list="dataList"
      :page-params="pageParams"
      :resize-observer-ele="resizeObserverEle"
      @handlePageChange="requestData"
      @selectionChange="selectionChange"
      @operationLog="operationLog"
    >
      <div slot="right" class="mune">
        <b>批量操作：</b>
        <el-button-group>
          <el-button
            v-if="$route.name === 'OnlineBuildingTasks'"
            size="mini"
            type="primary"
            round
            :disabled="btnDisabled.submit"
            @click="open('确定提交？', submit_edit)"
          >
            提 交
          </el-button>
          <el-button
            v-if="$route.name === 'OnlineBuildingTasks'"
            size="mini"
            type="primary"
            round
            :disabled="btnDisabled.reject"
            @click="open('确定撤回？', withdrawEdit, 50)"
          >
            撤 回
          </el-button>
          <el-button
            v-if="$route.name === 'OnlineBuildingTasks'"
            size="mini"
            type="primary"
            round
            :disabled="btnDisabled.abandon"
            @click="open('确定作废？', abandonEdit, 50)"
          >
            作 废
          </el-button>

          <el-button size="mini" type="primary" round :disabled="btnDisabled.again" @click="againChexk">推 送</el-button>
        </el-button-group>
      </div>
      <template v-slot:spuName="{ scope }">
        <span class="el-button--text el-button--mini" @click="goDetails(scope)">
          {{ menuName === 'newProducts' ? scope.row.spuName : scope.row.chgSpuName }}
        </span>
      </template>
    </ess-table>

    <purchaseLog v-model="spulogVisible" width="900px" :title="title" :thead-list="spuOperationLogTableHeader" :data-list="operationLogTableList" />
  </div>
</template>

<script>
import searchHead from '@//components/searchHead/searchHead.vue'
import essTable from '@/components/eTable/eTable.vue'
import purchaseLog from '@/components/dialogTableInfo/dialogTableInfo.vue'
import { spuListHeader } from '@/views/coi/commonJs/tableHeadList.js'
import { tableParams, searchHeaderEvent } from '@/components/eTable/js/mixins.js'
import { checkBoxFun } from '@/views/coi/commonJs/publicMethod.js'
import { requestTableDataFun, getHeaderMainStatus, pushFun, spuOperationLog, abandonFun, withdrawFun, submit_text } from '../js/maintain.js'
import bus from '@/utils/eventBus.js'
// import { initChgSpu} from '@/api/coi/onlineTaskAPI/spuEdit.js'
export default {
  components: {
    searchHead,
    essTable,
    purchaseLog
  },
  mixins: [
    tableParams,
    requestTableDataFun,
    searchHeaderEvent,
    getHeaderMainStatus,
    checkBoxFun,
    pushFun,
    spuOperationLog,
    abandonFun,
    withdrawFun,
    submit_text
  ],
  props: {
    menuName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      searchData: {},
      btnList: [
        { name: '查询', icon: 'el-icon-search', event: 'handleSearch' },
        { name: '重置', icon: 'el-icon-refresh', event: 'handleReset' }
      ],
      otherContainerBox: ['.app-container', '.checkComponent'],
      resizeObserverEle: ['.app-main', '.app-container'],
      spuListHeader,
      searchList: [
        { label: 'SPU名称', type: 'input', value: this.menuName === 'newProducts' ? 'spuName' : 'chgSpuName' },
        { label: '状态', type: 'select', value: 'mainStatus' },
        { label: 'UPC(不含检验码)', type: 'textarea', value: 'skuList' }
      ],
      options: {
        mainStatus: []
      },
      btnDisabled: {
        submit: true,
        reject: true,
        again: true,
        abandon: true
      }
    }
  },
  watch: {
    multipleSelection: {
      deep: true,
      handler(val) {
        if (Array.isArray(val) && val.length !== 0) {
          this.btnDisabled.submit = !val.every((item) => item.mainStatus === 201) // 采销维护
          this.btnDisabled.reject = !val.every((item) => item.mainStatus === 301 || item.mainStatus === 403 || item.mainStatus === 401)
          this.btnDisabled.again = !val.every((item) => item.mainStatus === 401 || item.mainStatus === 403)
          this.btnDisabled.abandon = !val.every((item) => item.mainStatus !== 998 && item.mainStatus !== 999)
        } else {
          this.btnDisabled.submit = true
          this.btnDisabled.reject = true
          this.btnDisabled.again = true
          this.btnDisabled.abandon = true
        }
      }
    }
  },
  methods: {
    async goDetails({ row }) {
      // console.log(this.$route.name) // SpuTeamDescription
      // return
      bus.$emit('setTagName', row[this.menuName === 'newProducts' ? 'spuName' : 'chgSpuName'])
      const mainStatus = row.mainStatus
      const id = row.id
      if (row.channelType && Number(row.channelType) === 12 && this.$route.name !== 'SpuTeamDescription') {
        const statusList = [101, 301, 401, 403, 999, 998]
        const query = {}
        let disabled = ''
        if (statusList.includes(mainStatus)) query.disabled = 'disabled'
        if (mainStatus === 301) {
          const { rolesTable } = this.$store.state.user
          const code = 'R017012'
          if (rolesTable.includes(code)) query.disabled = 'disabled'
        }
        // if(mainStatus === 301)
        // console.log(this.$store)
        query.status = 'edit'
        query.id = row.id
        disabled = query.disabled ? query.disabled : query.status
        query.menuName = this.menuName
        // console.log(this.menuName)
        if (this.menuName === 'safeguard') query.maintain = 'maintain'
        const url = `/addspu/${row.id}/${disabled}`
        this.$router.push({
          path: url,
          query
        })
        return
      }
      // const spuCode = row.spuCode
      // const { result } = await initChgSpu({ spuCode }, 'JDWX')
      // if (!result) return
      const query = {
        menuName: this.menuName,
        spuCode: id,
        isModify: 'true'
      }
      const statusList = [101, 301, 401, 403, 999, 998]
      if (statusList.includes(mainStatus)) query.isModify = 'not'
      if (this.$route.name === 'SpuTeamDescription') query.activeName = 'JD_channel'
      // 维护spu且是采销未校验过的走检验页面
      if (this.menuName === 'safeguard' && this.$route.name === 'OnlineBuildingTasks' && (row.mainStatus === 101 || row.mainStatus === 102)) {
        this.$router.push({
          path: `/SpuSeriesMaintain/${id}/${row.mainStatus}/${this.$route.name}`,
          query
        })
      } else {
        this.$router.push({
          path: `/spuSeries/${id}/${mainStatus}/${this.$route.name}`,
          query
        })
      }
    },
    againChexk() {
      console.log(this.$route.name, this.menuName)
      if (this.menuName === 'newProducts') this.open('确定更新到渠道？', this.textPushChannel, 50, false, 'id', true, 'spuId') // spu新增文描
      if (this.menuName === 'safeguard') this.open('确定更新到渠道？', this.textPushChannel_edit, 50, false, 'id', true, 'chgSpuId') // spu维护文描
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .table-header {
  padding-bottom: 8px;
}
</style>
